import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

class FragmentContentHandler extends DefaultHandler {
    String xPath = "/";
    XMLReader xmlReader;
    FragmentContentHandler parent;
    StringBuilder characters = new StringBuilder();
    Map<String, Integer> elementNameCount = new HashMap<String, Integer>();

    public FragmentContentHandler(XMLReader xmlReader) {
        this.xmlReader = xmlReader;
    }

    private FragmentContentHandler(String xPath, XMLReader xmlReader, FragmentContentHandler parent) {
        this(xmlReader);
        this.xPath = xPath;
        this.parent = parent;
    }

    @Override
    public void startElement(String uri, String localName, String qName,
                             Attributes atts) throws SAXException {
        Integer count = elementNameCount.get(qName);
        if (null == count) {
            count = 1;
        } else {
            count++;
        }
        elementNameCount.put(qName, count);
        String childXPath = xPath + "/" + qName + "[" + count + "]";
        int attsLength = atts.getLength();
        for (int x = 0; x < attsLength; x++) {
            System.out.println(childXPath + "[@" + atts.getQName(x) + "='"
                    + atts.getValue(x) + "'" + ']');
        }

        FragmentContentHandler child = new FragmentContentHandler(childXPath,
                xmlReader, this);
        xmlReader.setContentHandler(child);
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        String value = characters.toString().trim();
        if (value.length() > 0) {
            System.out.println(xPath + "='" + characters.toString() + "'");
        }
        xmlReader.setContentHandler(parent);
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        characters.append(ch, start, length);
    }
}

public class xmlpath {
    public static void main(String[] args) throws Exception {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        xr.setContentHandler(new FragmentContentHandler(xr));
        xr.parse(new InputSource(new FileInputStream("E:\\test.xml")));
    }
}
//eg: // ClinicalDocument[1]/component[1]/structuredBody[1]/component[9]/section[1]/entry[4]/observation[1]/value[1][@xsi:type='ST'] //ClinicalDocument[1]/component[1]/structuredBody[1]/component[9]/section[1]/entry[4]/observation[1]/value[1]='住院号' //ClinicalDocument[1]/component[1]/structuredBody[1]/component[9]/section[1]/entry[5]/organizer[1][@classCode='BATTERY'] //ClinicalDocument[1]/component[1]/structuredBody[1]/component[9]/section[1]/entry[5]/organizer[1][@moodCode='EVN'] //ClinicalDocument[1]/component[1]/structuredBody[1]/component[9]/section[1]/entry[5]/organizer[1]/component[1]/observation[1][@classCode='OBS'] //ClinicalDocument[1]/component[1]/structuredBody[1]/component[9]/section[1]/entry[5]/organizer[1]/component[1]/observation[1][@moodCode='EVN'] } }